<template>
    <div class="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
        <div class="max-w-md w-full space-y-8">
            <div>
                <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">创建新账户</h2>
            </div>
            <div class="mt-8 space-y-6">
                <div class="rounded-md shadow-sm">
                    <div class="mb-4">
                        <label for="username" class="sr-only">用户名</label>
                        <input v-model="username" type="text" required
                            class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm rounded-t-md"
                            placeholder="用户名">
                    </div>

                    <div class="mb-4">
                        <label for="password" class="sr-only">密码</label>
                        <input v-model="password" type="password" autocomplete="new-password" required
                            class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
                            placeholder="密码">
                    </div>

                    <div class="mb-4">
                        <label for="confirmPassword" class="sr-only">确认密码</label>
                        <input v-model="confirmPassword" type="password" autocomplete="new-password"
                            required
                            class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm"
                            placeholder="确认密码">
                    </div>
                    
                    <div class="mb-4">
                        <label for="email" class="sr-only">邮箱</label>
                        <input v-model="email" type="email" required
                        class="appearance-none rounded-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 focus:z-10 sm:text-sm rounded-b-md"
                        placeholder="邮箱">
                    </div>
                </div>

                <div class="flex items-center">
                    <input id="terms" name="terms" type="checkbox" required
                        class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded">
                    <label for="terms" class="ml-2 block text-sm text-gray-900">
                        我同意<a href="#" class="text-indigo-600 hover:text-indigo-500">服务条款</a>和<a href="#"
                            class="text-indigo-600 hover:text-indigo-500">隐私政策</a>
                    </label>
                </div>

                <div>
                    <button @click="handleRegister"
                        class="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
                        <span class="absolute left-0 inset-y-0 flex items-center pl-3">
                            <i class="fa fa-user-plus text-indigo-200 group-hover:text-indigo-100"></i>
                        </span>
                        创建账户
                    </button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import CryptoJS from 'crypto-js'

const router = useRouter()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const email = ref('')

const { Message } = useMessageBox()
const { register } = useApi()

const handleRegister = async() => {
    if (password.value != confirmPassword.value) {
        Message.error('密码不一致', { width: 'auto' })
        return
    }
    const response = await register({
        username: username.value,
        password: CryptoJS.SHA256(password.value).toString(CryptoJS.enc.Hex),
        email: email.value
    })
    if (response.code != 200) {
        Message.error(response.msg, { width: 'auto' })
    } else {
        router.push('/login') // 注册成功后跳转到登录页
        Message.success('注册成功', { width: 'auto' })
    }
    
}
</script>